package org.example.platform.elasticsearch.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.SearchException;
import org.example.platform.elasticsearch.entity.Hotel;
import org.example.platform.elasticsearch.entity.HotelDocRequest;
import org.example.platform.elasticsearch.service.ESDeleteDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class ESDeleteController {
    @Autowired
    ESDeleteDocService eSDeleteDocService;


    @PostMapping("/single/delete")
    public String singleDelete(String indexName, String docId) {

        try {
            String resultMap = eSDeleteDocService.singleDelete(indexName, docId);
            return resultMap;
        } catch (SearchException e) {
            log.warn("搜索发生异常，原因为:{}", e.getMessage());
            return "搜索发生异常，原因为:{}"+ e.getMessage();
        } catch (Exception e) {
            log.error("服务发生异常，原因为:{}", e.getMessage());
            return "服务发生异常，原因为:{}"+ e.getMessage();
        }
    }

    @PostMapping("/bulk/delete/doc")
    public String bulk(@RequestBody HotelDocRequest hotelDocRequest) throws Exception {
        List<Hotel> hotelList = hotelDocRequest.getHotelList();
        if (CollUtil.isEmpty(hotelList)) {
            return "无可修改文档";
        }
        String s = eSDeleteDocService.bulkDelete(hotelDocRequest);
        return s;
    }

    @PostMapping("/delete/byCity")
    public String deleteByQueryCity(String indexName,  String city) throws Exception {
        if(CharSequenceUtil.isBlank(indexName)){
            throw new Exception("索引名不能为空");
        }
        try {
            String resultMap = eSDeleteDocService.deleteByCity(indexName, city);
            return resultMap;
        } catch (SearchException e) {
            return "搜索发生异常，原因为:{}"+ e.getMessage();
        } catch (Exception e) {
            return "服务发生异常，原因为:{}"+ e.getMessage();
        }
    }
}
